from common.unit import AndroidUI
from common.home_page import HomePageObject
from common.qr_code import QRCodeX
from object.lnitial.lnitial_page import LnitialObject
from object.storage.packing_boxes import PackingObject
from object.storage.storage_page import StorageObject
from common.request_result import GetCode
import time


# 科室装袋-不开固定箱码-装箱操作
class PackingUnboxedTest:
    def __init__(self):
        self.unit = AndroidUI
        self.home = HomePageObject()
        self.qrcode = QRCodeX()
        self.ln = LnitialObject()
        self.get_code = GetCode()
        self.driver = AndroidUI.get_webdriver()
        self.pack = PackingObject()

    # 返回
    def back_button(self):
        self.driver.keyevent(4)

    # 判断是否登录，未登录就扫清理人二维码登录，已登录的情况下，不进行操作
    def check_login(self):
        # self.unit.scan(self.qrcode.zhaoqingli)
        try:
            self.ln.system_button()
            self.unit.scan(self.qrcode.zhaoqingli)
            print("登录成功")
        except:
            print("已经登录")

    # #通过点击进入装箱界面
    # def enter_box(self):
    #     try:
    #         self.home.enter_button().click()
    #         self.

    # 判断是否在装箱界面，如果是，就不操作，如果不是就扫垃圾袋二维码进入装箱界面(最新的垃圾袋会被装箱)
    def check_in_storage(self):
        try:
            self.pack.listbox_button()
            print("已经在装箱界面了")
        except:
            try:
                # 扫码最新的一条数据
                self.unit.scan(self.get_code.get_bag_cod(0))
                self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
                print("通过扫码最新一袋垃圾袋进入装箱界面成功")
                AndroidUI().write_result("通过扫码最新一袋垃圾袋进入装箱界面", "成功")
            except:
                print("通过扫码最新一袋垃圾袋进入装箱界面失败")
                AndroidUI.capture()
                AndroidUI().write_result("通过扫码最新一袋垃圾袋进入装箱界面", "失败")

    # 装箱操作，在装箱列表已经有垃圾的情况。进行装箱操作
    def pack_box_first(self):
        try:
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
            self.pack.pack_box_button().click()
            try:
                self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
                print('装箱界面在已有垃圾袋的情况下，装箱失败')
                AndroidUI.capture()
                AndroidUI().write_result("装箱界面在已有垃圾袋的情况下装箱", "失败")
            except:
                print('装箱界面在已有垃圾袋的情况下，装箱成功')
                AndroidUI().write_result("装箱界面在已有垃圾袋的情况下装箱", "成功")
        except:
            print("装箱界面的列表中没有垃圾袋")

    # 扫描从上往下排第n+1条垃圾袋的二维码
    def scan_bags(self, n):
        n2 = n + 1
        try:
            self.unit.scan(self.get_code.get_bag_cod(n))
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
            print(f"扫码垃圾袋{n2}成功")
            AndroidUI().write_result(f"扫码垃圾袋{n2}", "成功")
        except:
            print(f"扫码垃圾袋{n2}失败")
            AndroidUI.capture()
            AndroidUI().write_result(f"扫码垃圾袋{n2}", "失败")

    # 进行装箱操作
    def pack_box_second(self, x, y):

        for i in range(x, y):
            i2 = i + 1
            try:
                self.scan_bags(i)  # 扫码
                self.pack.pack_box_button().click()  # 点击装箱

                try:
                    self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
                    print(f'扫描垃圾袋{i2}，并装箱失败')
                    AndroidUI.capture()
                    AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱", "失败")
                except:
                    print(f'扫描垃圾袋{i2}并装箱，装箱成功')
                    AndroidUI().write_result("扫描垃圾袋并装箱", "成功")
            except:
                print("扫码装箱失败")
                AndroidUI.capture()
                AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱", "失败")

            # try:
            #     self.pack.listbox_button()
            #     print("进入装箱界面，并且扫码垃圾袋失败")
            #     AndroidUI.capture()
            #     AndroidUI().write_result("进入装箱界面，并且扫码垃圾袋", "失败")
            # except:
            #     self.scan_bags(i)       #扫码
            #     self.pack.pack_box_button().click()     #点击装箱
            #     AndroidUI().write_result("进入装箱界面，并且扫描垃圾袋", "成功")

        # try:
        #     self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
        #     self.pack.pack_box_button().click()
        #     try:
        #         self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
        #         print('装箱界面在已有垃圾袋的情况下，装箱失败')
        #         AndroidUI.capture()
        #         AndroidUI().write_result("装箱界面在已有垃圾袋的情况下装箱", "失败")
        #     except:
        #         print('装箱界面在已有垃圾袋的情况下，装箱成功')
        #         AndroidUI().write_result("装箱界面在已有垃圾袋的情况下装箱", "成功")
        #
        # except:
        #     print("装箱界面的列表中没有垃圾袋")

    # 扫描固定箱码
    def scan_fixed_box(self):
        self.unit.scan(self.qrcode.box_id(1))
        print("扫描固定箱码")

    def start_test(self, begin, end):
        # if box in "开启固定箱码":  # 此处得完善,box_first和box_second的代码,因为原来的点击装箱后就断言，需要另写方法或者优化方法
        #     print('开启固定箱码模式')
        #     self.check_login()
        #     # self.check_in_storage()
        #     self.pack_box_first()
        #     self.pack_box_second(begin, end)
        #     self.scan_fixed_box()  # 扫固定箱码
        # elif box in "关闭固定箱码":
        #     print('关闭固定箱码')
        self.check_login()
        # self.check_in_storage()
        # self.pack_box_first()         #不开固定箱码这个不用开
        self.pack_box_second(begin, end)


if __name__ == '__main__':
    test = PackingUnboxedTest()
    test.start_test(0, 2)
